<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Rest_Client - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.rest.client.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.rest.client.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.rest.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.rest.html">Zend_Rest</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.rest.server.html">Zend_Rest_Server</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.rest.client" class="section"><div class="info"><h1 class="title">Zend_Rest_Client</h1></div>
    

    <div class="section" id="zend.rest.client.introduction"><div class="info"><h1 class="title">Introduction</h1></div>
        

        <p class="para">
            Using the <span class="classname">Zend_Rest_Client</span> is very similar to using
            <code class="code">SoapClient</code> objects (<a href="http://www.php.net/soap" class="link external">&raquo; SOAP web service extension</a>).
            You can simply call the REST service procedures as
            <span class="classname">Zend_Rest_Client</span> methods. Specify the service&#039;s full
            address in the <span class="classname">Zend_Rest_Client</span> constructor.
        </p>

        <div class="example" id="zend.rest.client.introduction.example-1"><div class="info"><p><b>Example #1 A basic REST request</b></p></div>
            

            <pre class="programlisting brush: php">
/**
 * Connect to framework.zend.com server and retrieve a greeting
 */
$client = new Zend_Rest_Client(&#039;http://framework.zend.com/rest&#039;);

echo $client-&gt;sayHello(&#039;Davey&#039;, &#039;Day&#039;)-&gt;get(); // &quot;Hello Davey, Good Day&quot;
</pre>

        </div>

        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Differences in calling</b><br /></span>
            

            <p class="para">
                <span class="classname">Zend_Rest_Client</span> attempts to make remote methods
                look as much like native methods as possible, the only
                difference being that you must follow the method call with one
                of either  <span class="methodname">get()</span>,  <span class="methodname">post()</span>,
                 <span class="methodname">put()</span> or  <span class="methodname">delete()</span>. This call may
                be made via method chaining or in separate method calls:
            </p>

            <pre class="programlisting brush: php">
$client-&gt;sayHello(&#039;Davey&#039;, &#039;Day&#039;);
echo $client-&gt;get();
</pre>

        </p></blockquote>
    </div>

    <div class="section" id="zend.rest.client.return"><div class="info"><h1 class="title">Responses</h1></div>
        

        <p class="para">
            All requests made using <span class="classname">Zend_Rest_Client</span> return a
            <span class="classname">Zend_Rest_Client_Response</span> object. This object has many
            properties that make it easier to access the results.
        </p>

        <p class="para">
            When the service is based on <span class="classname">Zend_Rest_Server</span>,
            <span class="classname">Zend_Rest_Client</span> can make several assumptions about the
            response, including response status (success or failure) and return
            type.
        </p>

        <div class="example" id="zend.rest.client.return.example-1"><div class="info"><p><b>Example #2 Response Status</b></p></div>
            

            <pre class="programlisting brush: php">
$result = $client-&gt;sayHello(&#039;Davey&#039;, &#039;Day&#039;)-&gt;get();

if ($result-&gt;isSuccess()) {
    echo $result; // &quot;Hello Davey, Good Day&quot;
}
</pre>

        </div>

        <p class="para">
            In the example above, you can see that we use the request result as
            an object, to call  <span class="methodname">isSuccess()</span>, and then because of
             <span class="methodname">__toString()</span>, we can simply <code class="code">echo</code> the
            object to get the result. <span class="classname">Zend_Rest_Client_Response</span>
            will allow you to echo any scalar value. For complex types, you can
            use either array or object notation.
        </p>

        <p class="para">
            If however, you wish to query a service not using
            <span class="classname">Zend_Rest_Server</span> the
            <span class="classname">Zend_Rest_Client_Response</span> object will behave more like
            a <code class="code">SimpleXMLElement</code>. However, to make things easier, it
            will automatically query the <acronym class="acronym">XML</acronym> using XPath if the property is not
            a direct descendant of the document root element. Additionally, if
            you access a property as a method, you will receive the <acronym class="acronym">PHP</acronym> value
            for the object, or an array of <acronym class="acronym">PHP</acronym> value results.
        </p>

        <div class="example" id="zend.rest.client.return.example-2"><div class="info"><p><b>Example #3 Using Technorati&#039;s Rest Service</b></p></div>
            

            <pre class="programlisting brush: php">
$technorati = new Zend_Rest_Client(&#039;http://api.technorati.com/bloginfo&#039;);
$technorati-&gt;key($key);
$technorati-&gt;url(&#039;http://pixelated-dreams.com&#039;);
$result = $technorati-&gt;get();
echo $result-&gt;firstname() .&#039; &#039;. $result-&gt;lastname();
</pre>

        </div>

        <div class="example" id="zend.rest.client.return.example-3"><div class="info"><p><b>Example #4 Example Technorati Response</b></p></div>
            

            <pre class="programlisting brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!-- generator=&quot;Technorati API version 1.0 /bloginfo&quot; --&gt;
&lt;!DOCTYPE tapi PUBLIC &quot;-//Technorati, Inc.//DTD TAPI 0.02//EN&quot;
                      &quot;http://api.technorati.com/dtd/tapi-002.xml&quot;&gt;
&lt;tapi version=&quot;1.0&quot;&gt;
    &lt;document&gt;
        &lt;result&gt;
            &lt;url&gt;http://pixelated-dreams.com&lt;/url&gt;
            &lt;weblog&gt;
                &lt;name&gt;Pixelated Dreams&lt;/name&gt;
                &lt;url&gt;http://pixelated-dreams.com&lt;/url&gt;
                &lt;author&gt;
                    &lt;username&gt;DShafik&lt;/username&gt;
                    &lt;firstname&gt;Davey&lt;/firstname&gt;
                    &lt;lastname&gt;Shafik&lt;/lastname&gt;
                &lt;/author&gt;
                &lt;rssurl&gt;
                    http://pixelated-dreams.com/feeds/index.rss2
                &lt;/rssurl&gt;
                &lt;atomurl&gt;
                    http://pixelated-dreams.com/feeds/atom.xml
                &lt;/atomurl&gt;
                &lt;inboundblogs&gt;44&lt;/inboundblogs&gt;
                &lt;inboundlinks&gt;218&lt;/inboundlinks&gt;
                &lt;lastupdate&gt;2006-04-26 04:36:36 GMT&lt;/lastupdate&gt;
                &lt;rank&gt;60635&lt;/rank&gt;
            &lt;/weblog&gt;
            &lt;inboundblogs&gt;44&lt;/inboundblogs&gt;
            &lt;inboundlinks&gt;218&lt;/inboundlinks&gt;
        &lt;/result&gt;
    &lt;/document&gt;
&lt;/tapi&gt;
</pre>

        </div>

        <p class="para">
            Here we are accessing the <code class="code">firstname</code> and
            <code class="code">lastname</code> properties. Even though these are not
            top-level elements, they are automatically returned when accessed by
            name.
        </p>

        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Multiple items</b><br /></span>
            

            <p class="para">
                If multiple items are found when accessing a value by name, an
                array of SimpleXMLElements will be returned; accessing via
                method notation will return an array of <acronym class="acronym">PHP</acronym> values.
            </p>
        </p></blockquote>
    </div>

    <div class="section" id="zend.rest.client.args"><div class="info"><h1 class="title">Request Arguments</h1></div>
        

        <p class="para">
            Unless you are making a request to a <span class="classname">Zend_Rest_Server</span>
            based service, chances are you will need to send multiple arguments
            with your request. This is done by calling a method with the name of
            the argument, passing in the value as the first (and only) argument.
            Each of these method calls returns the object itself, allowing for
            chaining, or &quot;fluent&quot; usage. The first call, or the first argument
            if you pass in more than one argument, is always assumed to be the
            method when calling a <span class="classname">Zend_Rest_Server</span> service.
        </p>

        <div class="example" id="zend.rest.client.args.example-1"><div class="info"><p><b>Example #5 Setting Request Arguments</b></p></div>
            

            <pre class="programlisting brush: php">
$client = new Zend_Rest_Client(&#039;http://example.org/rest&#039;);

$client-&gt;arg(&#039;value1&#039;);
$client-&gt;arg2(&#039;value2&#039;);
$client-&gt;get();

// or

$client-&gt;arg(&#039;value1&#039;)-&gt;arg2(&#039;value2&#039;)-&gt;get();
</pre>

        </div>

        <p class="para">
            Both of the methods in the example above, will result in the
            following get args:
            <code class="code">?method=arg&amp;arg1=value1&amp;arg=value1&amp;arg2=value2</code>
        </p>

        <p class="para">
            You will notice that the first call of
            <code class="code">$client-&gt;arg(&#039;value1&#039;);</code> resulted in both
            <code class="code">method=arg&amp;arg1=value1</code> and <code class="code">arg=value1</code>;
            this is so that <span class="classname">Zend_Rest_Server</span> can understand the
            request properly, rather than requiring pre-existing knowledge of
            the service.
        </p>

        <div class="warning"><b class="warning">Warning</b><div class="info"><h1 class="title">Strictness of Zend_Rest_Client</h1></div>
            

            <p class="para">
                Any REST service that is strict about the arguments it receives will likely fail
                using <span class="classname">Zend_Rest_Client</span>, because of the behavior described
                above. This is not a common practice and should not cause problems.
            </p>
        </div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.rest.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.rest.html">Zend_Rest</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.rest.server.html">Zend_Rest_Server</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.rest.html">Zend_Rest</a></li>
  <li><a href="zend.rest.introduction.html">Introduction</a></li>
  <li class="active"><a href="zend.rest.client.html">Zend_Rest_Client</a></li>
  <li><a href="zend.rest.server.html">Zend_Rest_Server</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>